/*
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 *
 * Copyright @2008 the original author or authors.
 */
package com.bluestone.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import com.bluestone.util.Util;

/**
 * @author <a href="mailto:ming7655@gmail.com">Aaron</a>
 *
 * 
 */
@XmlRootElement(name="bluestone_cfg")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "bluestone_cfg", propOrder = {
	"actions",
	"assertors",
	"listeners",
	"actionHandlers",
	"expAttributesHandlers"

})
public class BluestoneConfig {
	
	@XmlElementWrapper(name = "actions")
	@XmlElement(name = "action")
	private List<ActionConfig> actions;
	
	@XmlElementWrapper(name = "assertors")
	@XmlElement(name = "assertor")
	private List<AssertorConfig> assertors;
	
	@XmlElementWrapper(name = "listeners")
	@XmlElement(name = "listener")
	private List<ListenerConfig> listeners;	
		
	@XmlElementWrapper(name = "actionHandlers")
	@XmlElement(name = "actionHandler")
	private List<ActionHandlerConfig> actionHandlers;
	
	@XmlElementWrapper(name = "expAttributesHandlers")
	@XmlElement(name = "expAttributesHandler")	
	private List<ExpAttributesHandlerConfig> expAttributesHandlers;
	
	public void putActionConfig(ActionConfig ac){
		if(actions==null){
			actions = new ArrayList();
		}
		if(ac==null){
			Util.warn("ActionConfig is null.");
			return;
		}
		if(ac.getType()==null||"".equals(ac.getType())){
			Util.warn("ActionConfig's type is null.");
			return;
		}
		this.actions.add(ac);
	}
	
	public void putActionHandlerConfig(ActionHandlerConfig ahc){
		if(actionHandlers==null){
			actionHandlers = new ArrayList();
		}
		if(ahc==null){
			Util.warn("ActionHandlerConfig is null.");
			return;
		}
		if(ahc.getType()==null||"".equals(ahc.getType())){
			Util.warn("ActionHandlerConfig's type is null.");
			return;
		}
		this.actionHandlers.add(ahc);
	}
	
	public void putExpAttributesHandlerConfig(ExpAttributesHandlerConfig eahc){
		if(expAttributesHandlers==null){
			expAttributesHandlers = new ArrayList();
		}
		if(eahc==null){
			Util.warn("ExpAttributesHandlerConfig is null.");
			return;
		}
		if(eahc.getTarget()==null||"".equals(eahc.getTarget())){
			Util.warn("ExpAttributesHandlerConfig's target is null.");
			return;
		}
		this.expAttributesHandlers.add(eahc);
	}
	
	public void putAssertorConfig(AssertorConfig ac){
		if(assertors==null){
			assertors = new ArrayList();
		}
		if(ac==null){
			Util.warn("AssertorConfig is null.");
			return;
		}
		if(ac.getName()==null||"".equals(ac.getName())){
			Util.warn("AssertorConfig's name is null.");
			return;
		}
		this.assertors.add(ac);
	}
	
	public ActionConfig getActionConfig(String type){
		if(actions!=null && actions.size()>0){
			for(ActionConfig ac:actions){
				if(ac.getType().equalsIgnoreCase(type)){
					return ac;
				}
			}
		}
		return null;
	}
	
	public ExpAttributesHandlerConfig getExpAttributeHandlerConfig(String target){
		if(expAttributesHandlers!=null && expAttributesHandlers.size()>0){
			for(ExpAttributesHandlerConfig eah: expAttributesHandlers){
				if(eah.getTarget().equalsIgnoreCase(target)){
					return eah;
				}
			}
		}
		return null;
	}

	public List getActionHandlers() {
		return actionHandlers;
	}

	public List getActions() {
		return actions;
	}

	public List getExpAttributesHandlers() {
		return expAttributesHandlers;
	}

	public List<AssertorConfig> getAssertors() {
		return assertors;
	}

	public void setAssertors(List<AssertorConfig> assertors) {
		this.assertors = assertors;
	}

	public List<ListenerConfig> getListeners() {
		return listeners;
	}

	public void setListeners(List<ListenerConfig> listeners) {
		this.listeners = listeners;
	}

}
